package com.carkeeper.billpay.service.impl;

import com.carkeeper.billpay.config.RabbitConfig;
import com.carkeeper.billpay.dao.PayLogMapper;
import com.carkeeper.billpay.pojo.PayLog;
import com.carkeeper.billpay.service.PayLogService;
import com.carkeeper.billpay.utils.TokenDecode;
import com.carkeeper.entity.Result;
import com.carkeeper.storebill.feign.StoreBillFeign;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PayLogServiceImpl implements PayLogService {

    @Autowired
    PayLogMapper payLogMapper;

    @Autowired
    StoreBillFeign storeBillFeign;

    @Autowired
    TokenDecode tokenDecode;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 根据订单号查询账单详情
     * @param id
     * @return 账单显示的组合实体
     */
    @Override
    public Result<Map> findBillCode(Integer id) {

        String billCode = payLogMapper.findBillCode(id);

        Result<Map> res = storeBillFeign.findByCode(billCode);
        return res;
    }

    /**
     * 已支付的支付单查询
     * @param searchMap
     * @return
     */
    @Override
    public PageInfo<PayLog> findPayLogListByCondition(Map<String, String> searchMap) {

        Example example = new Example(PayLog.class);
        Example.Criteria criteria = example.createCriteria();

        int pageSize = 20;
        int pageNum = 1;

        if (searchMap != null){

            if (searchMap.get("startPage") != null && searchMap.get("pageSize") != null){

                if (!"".equals(searchMap.get("startPage"))){
                    pageNum = Integer.parseInt(searchMap.get("startPage"));
                }

                if (!"".equals(searchMap.get("pageSize"))){
                    pageSize = Integer.parseInt(searchMap.get("pageSize"));
                }
            }
            PageHelper.startPage(pageNum,pageSize);

            //支付单号
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))){

                criteria.andEqualTo("id", searchMap.get("id"));
            }
            //收款人名称
            if (searchMap.get("payee") != null && !"".equals(searchMap.get("payee"))){

                criteria.andEqualTo("payee", searchMap.get("payee"));
            }
            //支付类型
            if (searchMap.get("payType") != null && !"".equals(searchMap.get("payType"))){

                criteria.andEqualTo("payType", searchMap.get("payType"));
            }
            //起始时间
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime"))){

                criteria.andGreaterThanOrEqualTo("payTime", searchMap.get("startTime"));
            }
            //终止时间
            if (searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime"))){

                criteria.andLessThanOrEqualTo("payTime", searchMap.get("endTime"));
            }
        }

        List<PayLog> payLogs = payLogMapper.selectByExample(example);

        PageInfo<PayLog> info = new PageInfo<>(payLogs);

        return info;

    }

    @Override
    public int addPayLog(PayLog payLog) {

        String username = tokenDecode.getUserInfo().get("username");

        if (payLog == null){
            throw new RuntimeException("支付不存在");
        }

        payLog.setPayee(username);
        payLog.setPayTime(new Date());
        String payType = payLog.getPayType();

        if ("1".equals(payType)){
            payLog.setRemarks("线下支付");
        }
        if ("2".equals(payType)){
            payLog.setRemarks("银行转账");
        }
        if ("3".equals(payType)){
            payLog.setRemarks("支票支付");
        }
        if ("4".equals(payType)){
            payLog.setRemarks("线下其他");
        }

        int i = payLogMapper.insertSelective(payLog);
        if (i >=0){
            rabbitTemplate.convertAndSend(RabbitConfig.EX_STORE_BILL_PAY,"",payLog.getCode());
            return 1;
        }
        return 0;

    }

    @Override
    public int findTotalMoneyByDate(String date) {

        Example example = new Example(PayLog.class);
        Example.Criteria criteria = example.createCriteria();
        int totalMoney = 0;

        if (date != null && date.length() > 0){

            criteria.andEqualTo("createTime", date);
        }

        List<PayLog> payLogs = payLogMapper.selectByExample(example);

        for (PayLog payLog : payLogs) {

            totalMoney+=payLog.getPayMoney();
        }

        return totalMoney;
    }
}
